import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:hktk_app/dao/login_dao.dart';
import 'package:hktk_app/dao/my_dao.dart';
import 'package:hktk_app/model/login/user_info_model.dart';
import 'package:hktk_app/pages/mine/question_bank_page.dart';

import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/my_util.dart';
import 'package:hktk_app/util/screen_adapter_helper.dart';
import 'package:hktk_app/util/toast_util.dart';

import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/mine/my_tab_widget.dart';
import 'package:url_launcher/url_launcher.dart';

// import 'package:hktk_app/pages/mine/activation_page.dart';
// import 'package:hktk_app/pages/mine/customer_service_page.dart';
// import 'package:hktk_app/pages/mine/download_page.dart';
// import 'package:hktk_app/pages/mine/my_course_page.dart';
// import 'package:hktk_app/pages/mine/opinion_page.dart';
// import 'package:hktk_app/pages/mine/question_bank_page.dart';
// import 'package:hktk_app/pages/mine/resource_page.dart';
// import 'package:hktk_app/pages/mine/set_up_page.dart';
// import 'package:path/path.dart' as path;

class MinePage extends StatefulWidget {
  const MinePage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _createMinePageState();
}

class _createMinePageState extends State<MinePage> with WidgetsBindingObserver, AutomaticKeepAliveClientMixin {
  late BuildContext _context;
  late UserInfoModel _userInfo;
  final List<Map<String, dynamic>> _list = [
    {
      "pngUrl": "images/my/order1.png",
      "name": "订单",
      "pageName": "/mine/orderList"
    }
  ];

  final List<Map<String, dynamic>> _Resources = [
    {
      "svgUrl": "images/my/tab2_tk.svg",
      "name": "题库",
      "pageName": "/mine/questionBank",
    },
    {
      "svgUrl": "images/my/tab2_kc.svg",
      "name": "课程",
      "pageName": "/mine/course"
    },
    {
      "svgUrl": "images/my/tab2_zl.svg",
      "name": "资料",
      "pageName": "/mine/resource"
    },
    {
      "svgUrl": "images/my/tab2_qk.svg",
      "name": "清空做题记录",
      "onPage": null,
      "pageName": null
    },
    {
      "svgUrl": "images/my/tab2_tk.svg",
      "name": "错题记录",
      "pageName": "/wrong-question",
    },
    {
      "svgUrl": "images/my/tab2_kc.svg",
      "name": "错题精选",
      "pageName": "/error-primary"
    },
    {
      "svgUrl": "images/my/tab2_zl.svg",
      "name": "热点试题",
      "pageName": "/hot-question"
    },
    {
      "svgUrl": "images/my/tab2_qk.svg",
      "name": "我的收藏",
      "pageName": "/my-collection"
    },
  ];

  final List<Map<String, dynamic>> _OtherFunctionList = [
    // {
    //   "svgUrl": "images/my/other1.svg",
    //   "name": "激活",
    //   "pageName": "/mine/active"
    // },
    {
      "svgUrl": "images/my/other1.svg",
      "name": "操作指引",
      "pageName": "/action-guide"
    },
    {
      "svgUrl": "images/my/other2.svg",
      "name": "下载",
      "pageName": "/mine/download"
    },
    {
      "svgUrl": "images/my/other2.svg",
      "name": "离线视频",
      "pageName": "/save-video-list"
    },
    {
      "svgUrl": "images/my/other4.svg",
      "name": "在线客服",
      "pageName": "/mine/customer_service"
    },
    // {
    //   "svgUrl": "images/my/other5.svg",
    //   "name": "商务合作",
    //   // "onPage": const ResourcePage()
    //   "pageName": null,
    //   "callback": (BuildContext context) async {
    //     var configModel = await LoginDao.config();
    //     String url = configModel?.appConfig?.customerServiceLink ?? "";
    //     if (url != "") {
    //       bool cna = await canLaunchUrl(Uri.parse(url));
    //       if (cna) {
    //         launchUrl(Uri.parse(url)).catchError((err) {
    //           ToastUtil.show(context: context, msg: "未知错误，请重试");
    //         });
    //       } else {
    //         ToastUtil.show(context: context, msg: "未找到微信，请先安装微信");
    //       }
    //     } else {
    //       ToastUtil.show(context: context, msg: "获取链接失败，请退出重试");
    //     }
    //   }
    // },
    {
      "svgUrl": "images/my/other3.svg",
      "name": "意见反馈",
      "pageName": "/mine/option"
    },
  ];

  @override
  void initState() {
    //   initState
    super.initState();
    // WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
    //   // 只有index为0的tab需要此操作，再次渲染，以获取正确的bottomBar高度，请求不用重新执行
    //   setState(() {
    //   });
    // });
    _userInfo = getUserInfo();

    //   判断是不是ios
    () async {
      var showIosPay = await MyDao.versionCurrentIos();
      if (!showIosPay) {
        _OtherFunctionList.insert(0, {
          "svgUrl": "images/my/other1.svg",
          "name": "激活",
          "pageName": "/mine/active"
        });
        if (mounted) {
          setState(() {});
        }
      }
    }();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    // String s=path.join("assets", "'symbols'");
    // debugPrint("main 页面 -> initializeDateFormatting 方法执行 ===> "+s);
    // ScreenUtil() ;
    // debugPrint(1.sh.toString());
    _context = context;
    // 
    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment(0, 0.00),
          end: Alignment(0, 1),
          // [Color(0xFF9CC2FF), Color(0xFFD4E5FF), Color(0xFFF6F6F6)]
          colors: [Color(0x7F9DC2FF), Color(0x7FD5E5FF), Color(0x7FFFFF)],
        ),
      ),
      child: SafeArea(
          child: Container(
        height: 1.sh -
            ScreenUtil().statusBarHeight -
            ScreenHelper.bottomNavigationBarHeight,
        child: ListView(
          children: [
            hiSpace(height: 20),
            Align(
              alignment: Alignment.centerRight,
              child: Container(
                width: 100.w,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    GestureDetector(
                        onTap: () {
                          Navigator.pushNamed(context, "/setUp");
                        },
                        child: Container(
                          // width: 100.w,
                          height: 30.h,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              SvgPicture.asset("images/my/tab2_sz.svg",
                                  width: 20.w, height: 13.h),
                              SizedBox(
                                width: 50.w,
                                height: 13.h,
                                child: Text(
                                  '设置',
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                    color: Color(0xFF555555),
                                    fontSize: 10.sp,
                                    fontWeight: FontWeight.w400,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ))
                  ],
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.symmetric(horizontal: 40),
              child: Row(
                children: [
                  SizedBox(
                    width: 66,
                    height: 66,
                    child: Stack(
                      children: [
                        Positioned.fill(
                          child: ClipOval(
                            child: _userInfo.header != ""
                                ? Image.network(
                                    _userInfo.header!,
                                    width: 66,
                                    height: 66,
                                    fit: BoxFit.cover,
                                  )
                                : Image.asset(
                                    "images/my/head.png",
                                    width: 66,
                                    height: 66,
                                    fit: BoxFit.cover,
                                  ),
                          ),
                        ),
                        // Positioned(bottom: 0, right: px6, child: _iconItem()),
                      ],
                    ),
                  ),
                  hiSpace(width: 20),
                  Padding(
                    padding: EdgeInsets.symmetric(vertical: 7),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(
                              _userInfo.name,
                              style: FontUtil.fontBlack1(18,
                                  fontWeight: FontWeight.w600),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            SvgPicture.asset(
                              "images/my/edit.svg",
                              width: 10.w,
                              height: 10.w,
                            )
                          ],
                        ),
                        Text(
                          "精选荟萃，考试无忧！",
                          style: FontUtil.fontGrey9(12),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            hiSpace(height: 40),
            MyTabWidget(
              title: "我的订单",
              tabList: _list,
              ykdlFun: () {},
              showBackground: true,
            ),
            hiSpace(height: 30),
            MyTabWidget(
              title: "我的资源",
              tabList: _Resources,
              ykdlFun: () {},
              showBackground: true,
            ),
            hiSpace(height: 30),
            Padding(
              padding: EdgeInsets.only(left: 30, right: 30, bottom: 30),
              child: Container(
                  decoration: BoxDecoration(
                    color: colorWhite1,
                    borderRadius: BorderRadius.all(Radius.circular(16)),
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      for (var map in _OtherFunctionList)
                        Container(
                            padding: EdgeInsets.only(right: 10.w),
                            child: GestureDetector(
                                onTap: () {
                                  if (map['pageName'] == null) {
                                    if (map.containsKey("callback")) {
                                      map['callback']();
                                    }
                                  } else {
                                    Navigator.pushNamed(
                                        context, map['pageName']);
                                  }
                                },
                                child: Row(
                                  // mainAxisAlignment:
                                  //     MainAxisAlignment.spaceBetween,
                                  mainAxisSize: MainAxisSize.max,
                                  children: [
                                    Expanded(
                                        child: Container(
                                      padding: EdgeInsets.all(10.w),
                                      child: Row(
                                        children: [
                                          SvgPicture.asset(map['svgUrl']),
                                          SizedBox(
                                            width: 8.w,
                                          ),
                                          Expanded(child: Text(map['name'])),
                                        ],
                                      ),
                                    )),
                                    SvgPicture.asset(
                                      "images/my/toRight.svg",
                                      width: 16,
                                    )
                                  ],
                                )))
                    ],
                  )),
            )
          ],
        ),
      )),
    );
  }

  @override
  //   wantKeepAlive
  bool get wantKeepAlive => true;
}
